package model;

import java.io.IOException;
import java.util.List;

import javax.bluetooth.RemoteDevice;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;

public class Server
    implements
        Runnable
{

    private UUID uuid;
    private String url;
    private StreamConnectionNotifier scnotifier;
    private StreamConnection sc;
    private Envelope envelope;

    public Server(
        String urlString, Envelope envelope )
    {
        uuid = new UUID( urlString, true );
        url = "btspp://localhost:" + uuid + ";name=Server SSP";
        this.envelope = envelope;
    }

    @Override
    public void run()
    {
        RemoteDevice remoteDevice;
        try {
            scnotifier = (StreamConnectionNotifier) Connector.open( url );
            System.out.println( "Server: esperando as conexões" );
            while( true ) {
                sc = scnotifier.acceptAndOpen();
                remoteDevice = RemoteDevice.getRemoteDevice( sc );
                
                //envelope.set( remoteDevice );
                
                System.out.println( "Server: conectei o " + remoteDevice.getFriendlyName( true )
                    + " de endereço " + remoteDevice.getBluetoothAddress() );
                
                //List<RemoteDevice> lista = envelope.get();
                
                //System.out.println("conectados: "+lista.size());
            }
        } catch( IOException e ) {
            System.out.println( "Server: tentativa de abrir conexão mal sucedida" );
        }
    }
}